package com.njym.java.security.app.rest;

import com.alipay.api.AlipayApiException;
import com.alipay.api.AlipayClient;
import com.alipay.api.DefaultAlipayClient;
import com.alipay.api.request.AlipayFundTransToaccountTransferRequest;
import com.alipay.api.response.AlipayFundTransToaccountTransferResponse;
import com.google.gson.Gson;
import com.njym.java.security.app.alipay.AliPayProperties;
import com.njym.java.security.app.biz.CompanyIdentificationBiz;
import com.njym.java.security.app.biz.PersonalIdentificationBiz;
import com.njym.java.security.app.entity.Alipay;
import com.njym.java.security.app.entity.CompanyIdentification;
import com.njym.java.security.app.entity.PersonalIdentification;
import com.njym.java.security.common.rest.BaseController;
import com.njym.java.security.common.service.CaptchaCodeManager;
import com.njym.java.security.common.util.ResponseUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import tk.mybatis.mapper.entity.Example;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("personalIdentification")
public class PersonalIdentificationController extends BaseController<PersonalIdentificationBiz,PersonalIdentification> {
    @Autowired
    private CompanyIdentificationBiz companyIdentificationBiz;
    /**
     * 个人认证
     * @param code
     * @param name
     * @param cardNo
     * @param mobile
     * @param frontImage
     * @param contraryImage
     * @return
     */
    @RequestMapping(value = "/submit", method = RequestMethod.GET)
    @ResponseBody
    public Object submit(String code, String name, String cardNo, String mobile, String frontImage, String contraryImage, String identity, String userId) {
        //判断验证码是否正确
        if (code != null) {
            //判断验证码是否正确
            String cacheCode = CaptchaCodeManager.getCachedCaptcha(mobile);
            if (cacheCode == null || cacheCode.isEmpty() || !cacheCode.equals(code)) {
                return ResponseUtil.fail(-1, "验证码错误");
            }
        }

        Example example = new Example(PersonalIdentification.class);
        example.createCriteria().andEqualTo("userId", userId).andEqualTo("status", 2).andEqualTo("deleted", 0);
        List<PersonalIdentification> list = baseBiz.selectByExample(example);

        Example example1 = new Example(CompanyIdentification.class);
        example1.createCriteria().andEqualTo("userId", userId).andEqualTo("status", 2).andEqualTo("deleted", 0);
        List<CompanyIdentification> lists = companyIdentificationBiz.selectByExample(example1);

        if(list.size() > 0 || lists.size() > 0){
            return ResponseUtil.fail(-1, "您已申请，无需重复申请！");
        }

        PersonalIdentification personalIdentification = new PersonalIdentification();
        if (!identity.equals("undefined") && identity != null) {
            Integer iden = Integer.parseInt(identity);
            personalIdentification.setIdentity(iden);
        }
        if (!userId.equals("undefined") && userId != null) {
            Integer uid = Integer.parseInt(userId);
            personalIdentification.setUserId(uid);
        }
        personalIdentification.setName(name);
        personalIdentification.setCardNo(cardNo);
        personalIdentification.setContraryImage(contraryImage);
        personalIdentification.setFrontImage(frontImage);
        personalIdentification.setMobile(mobile);
        personalIdentification.setStatus(1);

        Example example2 = new Example(PersonalIdentification.class);
        example2.createCriteria().andEqualTo("userId", userId).andEqualTo("status", 1).andEqualTo("deleted", 0);
        List<PersonalIdentification> per = baseBiz.selectByExample(example2);
        if(per.size() > 0){
            personalIdentification.setId(per.get(0).getId());
            baseBiz.updateSelectiveById(personalIdentification);
        }else{
            baseBiz.insertSelective(personalIdentification);
        }

        return ResponseUtil.ok();
    }


    @RequestMapping(value = "/queryApply", method = RequestMethod.GET)
    @ResponseBody
    public Object queryApply(String userId) {

        Example example = new Example(PersonalIdentification.class);
        example.createCriteria().andEqualTo("userId", userId).andEqualTo("status", 2).andEqualTo("deleted", 0);
        List<PersonalIdentification> list = baseBiz.selectByExample(example);

        Example example1 = new Example(CompanyIdentification.class);
        example1.createCriteria().andEqualTo("userId", userId).andEqualTo("status", 2).andEqualTo("deleted", 0);
        List<CompanyIdentification> lists = companyIdentificationBiz.selectByExample(example1);

        Map<String, Object> data = new HashMap<>(2);
        data.put("personal", list);
        data.put("company", lists);
        return ResponseUtil.ok(data);
    }

    @RequestMapping(value = "/changeIdentification", method = RequestMethod.GET)
    @ResponseBody
    public Object changeIdentification(String code, String name, String cardNo, String mobile, String frontImage, String contraryImage, String identity, String userId) {
        //判断验证码是否正确
        if (code != null) {
            //判断验证码是否正确
            String cacheCode = CaptchaCodeManager.getCachedCaptcha(mobile);
            if (cacheCode == null || cacheCode.isEmpty() || !cacheCode.equals(code)) {
                return ResponseUtil.fail(-1, "验证码错误");
            }
        }

        PersonalIdentification personalIdentification = new PersonalIdentification();
        if (!identity.equals("undefined") && identity != null) {
            Integer iden = Integer.parseInt(identity);
            personalIdentification.setIdentity(iden);
        }
        if (!userId.equals("undefined") && userId != null) {
            Integer uid = Integer.parseInt(userId);
            personalIdentification.setUserId(uid);
        }
        personalIdentification.setName(name);
        personalIdentification.setCardNo(cardNo);
        personalIdentification.setContraryImage(contraryImage);
        personalIdentification.setFrontImage(frontImage);
        personalIdentification.setMobile(mobile);
        personalIdentification.setStatus(1);


        Example example = new Example(PersonalIdentification.class);
        example.createCriteria().andEqualTo("userId", userId).andEqualTo("status", 2).andEqualTo("deleted", 0);
        List<PersonalIdentification> per = baseBiz.selectByExample(example);

        Example example1 = new Example(CompanyIdentification.class);
        example1.createCriteria().andEqualTo("userId", userId).andEqualTo("status", 2).andEqualTo("deleted", 0);
        List<CompanyIdentification> com = companyIdentificationBiz.selectByExample(example1);

        if(per.size() > 0){
            personalIdentification.setId(per.get(0).getId());
            baseBiz.updateSelectiveById(personalIdentification);
        }else{
            if(com.size() > 0){
                CompanyIdentification c = com.get(0);
                c.setDeleted(1);
                companyIdentificationBiz.updateSelectiveById(c);
                baseBiz.insertSelective(personalIdentification);
            }
        }


        return ResponseUtil.ok();
    }

}